package com.slipper.system.menu.service;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.system.codeless.bo.PageBO;
import com.slipper.system.menu.bo.MenuBO;
import com.slipper.system.menu.bo.PageMenuBO;
import com.slipper.system.codeless.service.PageService;
import com.slipper.util.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
@ReadTransactional
public class PageMenuService {
    @Resource
    private MenuService menuService;
    @Resource
    private PageService pageService;

    @WriteTransactional
    public Integer save(PageMenuBO bo) {
        if (MenuBO.TYPE_PAGE.equals(bo.getType()) && bo.getPage() != null) {
            pageService.save(bo.getPage());
            bo.setPageId(bo.getPage().getId());
            if (PageBO.TYPE_VISUAL.equals(bo.getPage().getType())
                    && PageBO.LAYOUT_MODE_DASHBOARD.equals(bo.getPage().getLayoutMode())) {
                bo.setType(MenuBO.TYPE_DASHBOARD);
            }
        } else if (MenuBO.TYPE_LINk_PAGE.equals(bo.getType()) && bo.getPageId() != null) {
            PageBO page = pageService.queryBy(bo.getPageId());
            if (PageBO.TYPE_VISUAL.equals(page.getType())
                    && PageBO.LAYOUT_MODE_DASHBOARD.equals(page.getLayoutMode())) {
                bo.setType(MenuBO.TYPE_DASHBOARD);
            } else {
                bo.setType(MenuBO.TYPE_PAGE);
            }
        }

        MenuBO menuBO = BeanUtil.copy(bo, MenuBO.class);
        menuService.save(menuBO);
        return 1;
    }
}
